package com.example.demo.controller;

import com.example.demo.entity.Books;
import com.example.demo.service.MyService;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * @description:
 * @author: ly
 * @create: 14-06-28 14:11:48
 */
@RequestMapping("/my")
@RestController
public class MyController {

    @Resource
    MongoTemplate mongoTemplate;


    @Resource
    MyService myService;

    @GetMapping("/test01")
    public ResponseEntity test01(String name, String password) {
        System.out.println( name );
        System.out.println( password );
        return ResponseEntity.ok( name + "ok" + password );

    }

    @RequestMapping("/test02")
    public Books test02(@RequestParam("name") String name) {
        return myService.getBookById( name );
    }

    @RequestMapping("/test03")
    public List<Books> test03(@RequestParam("name") String name) {
        return myService.getBooksByName( name );
    }

    @RequestMapping("/test04")
    public List<Books> test04(@RequestParam("name") String name) {
        BigDecimal bigDecimal = new BigDecimal( 6.2 );
        return myService.getBooksByName( name );
    }

    public static void main(String[] args) throws Exception {
        Document document = new Document();///D:/cj/code/idea/demo/target/classes/json/
//        Path path = Paths.get(ClassLoader.getSystemResource( "json/img.jpg" ).toURI());
        Path path = Paths.get(ClassLoader.getSystemResource( "json/img.jpg" ).toURI());
        PdfWriter.getInstance(document, new FileOutputStream("iTextHelloWorld.pdf"));

        document.open();
        Font font = FontFactory.getFont(FontFactory.COURIER, 16, BaseColor.BLACK);
        Chunk chunk = new Chunk("Hello World", font);
        document.open();
        Image img = Image.getInstance(path.toAbsolutePath().toString());
        document.add(img);
        document.add(chunk);
        document.close();
    }

}
